package apis

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"net/http"
	"tuowei.com/app/admin/models"
	"tuowei.com/app/teacher/service"
	"tuowei.com/app/teacher/service/dto"
	"tuowei.com/app/teacher/service/response"
	"tuowei.com/pkg/xlog"
)

type Class struct {
	api.Api
	base
}
type status struct {
	Status     int    `json:"status"`
	StatusName string `json:"status_name"`
}

func (c Class) GetCondition(ctx *gin.Context) {
	user := c.GetTeacherInfo(ctx)
	s := service.TeachingPointService{}
	c.MakeContext(ctx).MakeOrm().MakeService(&s.Service)
	areas := s.GetCondition(user.SysUser.UserId)
	status := []status{
		{1, "运行中"},
		{2, "已结业"},
	}
	ctx.JSON(http.StatusOK, map[string]interface{}{
		"code": 200,
		"msg":  "success",
		"data": map[string]interface{}{
			"areas":  areas,
			"status": status,
		},
	})
}

func (c Class) GetAllLists(ctx *gin.Context) {
	s := service.ClassService{}
	err := c.MakeContext(ctx).MakeOrm().MakeService(&s.Service).Errors
	if err != nil {
		c.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	teacherInfo := c.GetTeacherInfo(ctx)
	lists := make([]models.TeachingPoint, 0)
	err = s.GetAllLists(&lists, teacherInfo.SysUser.UserId)
	if err != nil {
		c.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	c.OK(lists, "success")
}

func (c Class) GetLists(ctx *gin.Context) {
	teacher := c.GetTeacherInfo(ctx)
	s := service.ClassService{}
	req := dto.GetClassListsRequest{}
	//user := c.GetTeacherInfo(ctx)
	err := c.MakeContext(ctx).MakeOrm().MakeService(&s.Service).Bind(&req, binding.Default(ctx.Request.Method, ctx.ContentType())).Errors
	if err != nil {
		c.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	lists := make([]models.SysClassList, 0)
	var count int64
	s.GetLists(&lists, &count, &req, teacher.SysUser.UserId)
	for k := range lists {
		if lists[k].Dept != nil {
			lists[k].AreaName = lists[k].Dept.Name
		}
	}
	c.PageOK(lists, int(count), req.GetPageIndex(), req.GetPageSize(), "success")
	//userId := user.SysUser.UserId
	//userDeptId := user.SysUser.DeptId

}

func (c Class) Detail(ctx *gin.Context) {
	req := dto.ClassDetailRequest{}
	err := c.MakeContext(ctx).MakeOrm().Bind(&req).Errors
	if err != nil {
		c.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	detail := models.SysClass{}
	err = c.Orm.Where("id = ?", req.Id).Preload("Dept").First(&detail).Error
	if err != nil {
		c.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	rsp := &response.ClassDetailResponse{}
	rsp.Make(detail)
	c.OK(rsp, "success")
}

func (c Class) GetClassStudents(ctx *gin.Context) {
	req := dto.ClassStudentRequest{}
	err := c.MakeContext(ctx).MakeOrm().Bind(&req, binding.Default(ctx.Request.Method, ctx.ContentType())).Errors
	if err != nil {
		c.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	classDetail := models.SysClass{}
	err = c.Orm.Where("id = ?", req.Id).First(&classDetail).Error
	if err != nil {
		c.Error(http.StatusInternalServerError, errors.New("班级不存在"), "班级不存在")
		return
	}
	students := make([]models.SysStudent, 0)
	db := c.Orm.Preload("WxStudents").Preload("QqStudents").Where("class_id = ?", req.Id)
	if req.Name != "" {
		db = db.Where("name = ?", req.Name)
	}
	err = db.Find(&students).Error
	if err != nil {
		c.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	stus := make([]response.ClassStudentsResponse, 0)
	for _, v := range students {
		avator := ""
		if v.WxStudents != nil && v.WxStudents.Avatar != "" {
			avator = v.WxStudents.Avatar
		} else if v.QqStudents != nil && v.QqStudents.FigureurlQq2 != "" {
			avator = v.QqStudents.FigureurlQq2
		}
		stus = append(stus, response.ClassStudentsResponse{
			Id:       v.Id,
			Name:     v.Name,
			IdCard:   v.IdCard,
			Avator:   avator,
			UserType: v.UserType,
		})
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "success",
		"data": map[string]interface{}{
			"class_name": classDetail.Name,
			"students":   stus,
		},
	})
}

func (c Class) RemoveStudents(ctx *gin.Context) {
	req := dto.RemoveStudentsRequest{}
	err := c.MakeContext(ctx).MakeOrm().Bind(&req, binding.JSON).Errors
	if err != nil {
		c.Error(http.StatusInternalServerError, err, err.Error())
		return
	}
	// 更新学员状态
	c.Orm.Table(models.SysStudent{}.TableName()).
		Where("class_id = ?", req.ClassId).
		Where("id in (?)", req.StudentIds).Update("class_id", 0)
	xlog.Pretty(req)
}
